<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Demo-15-内置指令</title>
    <script type="text/javascript" src="../JS/vue.js"></script>
</head>
<body>
<div id="root">
    <!-- 我们之前学过的指令:
        v-bind  : 单向绑定解析表达式, 简写 :xxx
        v-model : 双向数据绑定
        v-for   : 遍历数组, 对象
        v-on    : 绑定事件监听, 简写: @xxx
        v-if    : 条件渲染
        v-show  : 条件渲染 -->
    名称: <input type="text" v-bind:value="name">
    名称: <input type="text" :value="name">
    <br><br>

    名称: <input type="text" v-model="name">
    <br><br>

    <h3>爱好</h3>
    <ul>
        <li v-for="(hobby, index) in hobbys" :key="index">
            {{ hobby }}
        </li>
    </ul>
    <br><br>

    <button v-on:click.once="addHobby('玩电脑')">添加一个爱好</button>
    <button @click.once="deleteHobby(2)">删除一个爱好</button>
    <br><br>

    <template v-if="hobbys.length === 3">
        <h3>现在有三个爱好</h3>
    </template>
    <br><br>

    <!-- 有Bug -->
    <template v-show="hobbys.length === 4">
        <h3>现在有四个爱好</h3>
    </template>
</div>

<script text="text/javascript">
    const vm = new Vue({
        el: '#root',

        data: {
            name: 'Blue',
            hobbys: [
                '抽烟',
                '喝酒',
                '烫头',
            ]
        },

        methods: {
            addHobby(hobby) {
                this.hobbys.push(hobby)
            },

            deleteHobby(index) {
                this.hobbys.splice(index, 1)
            }
        }
    })
</script>
</body>
</html>